package com.company.oa.meeting.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.oa.common.api.Result;
import com.company.oa.meeting.dto.MeetingRoomDTO;
import com.company.oa.meeting.entity.MeetingRoom;
import com.company.oa.meeting.service.MeetingRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 会议室控制器
 */
@RestController
@RequestMapping("/meeting-rooms")
public class MeetingRoomController {

    @Autowired
    private MeetingRoomService meetingRoomService;

    /**
     * 分页查询会议室
     */
    @GetMapping("/page")
    public Result<IPage<MeetingRoom>> getMeetingRoomPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            MeetingRoom meetingRoom) {
        IPage<MeetingRoom> pageResult = meetingRoomService.getMeetingRoomPage(page, size, meetingRoom);
        return Result.success(pageResult);
    }

    /**
     * 获取会议室详情
     */
    @GetMapping("/{id}")
    public Result<MeetingRoom> getMeetingRoomDetail(@PathVariable Long id) {
        MeetingRoom meetingRoom = meetingRoomService.getById(id);
        if (meetingRoom == null) {
            return Result.failed("会议室不存在");
        }
        return Result.success(meetingRoom);
    }

    /**
     * 添加会议室
     */
    @PostMapping
    public Result<Boolean> addMeetingRoom(@RequestBody @Valid MeetingRoomDTO meetingRoomDTO) {
        boolean result = meetingRoomService.addMeetingRoom(meetingRoomDTO);
        return Result.success(result);
    }

    /**
     * 修改会议室
     */
    @PutMapping
    public Result<Boolean> updateMeetingRoom(@RequestBody @Valid MeetingRoomDTO meetingRoomDTO) {
        if (meetingRoomDTO.getId() == null) {
            return Result.failed("会议室ID不能为空");
        }
        boolean result = meetingRoomService.updateMeetingRoom(meetingRoomDTO);
        return Result.success(result);
    }

    /**
     * 删除会议室
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteMeetingRoom(@PathVariable Long id) {
        boolean result = meetingRoomService.deleteMeetingRoom(id);
        return Result.success(result);
    }

    /**
     * 切换会议室状态
     */
    @PostMapping("/{id}/toggle-status")
    public Result<Boolean> toggleStatus(@PathVariable Long id) {
        boolean result = meetingRoomService.toggleStatus(id);
        return Result.success(result);
    }

    /**
     * 获取可用的会议室
     */
    @GetMapping("/available")
    public Result<List<MeetingRoom>> getAvailableRooms(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @RequestParam(required = false) Integer capacity) {
        List<MeetingRoom> rooms = meetingRoomService.getAvailableRooms(startTime, endTime, capacity);
        return Result.success(rooms);
    }
}